using System;
using System.Collections.Generic;
using System.Text;
using WatiN.Core;

namespace Wax
{

    /// <summary>
    /// Used to perform commands specific to <see cref="TextField"/> elements.
    /// </summary>
    public class TextFieldCommand : Command
    {
        /// <summary>
        /// Executes this command
        /// </summary>
        /// <param name="context">The execution context used to execute this command</param>
        [CLSCompliant(false)]
        public override void Execute(IExecutionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            switch (this.CommandType)
            {
                case CommandType.TypeText:
                    ExecuteTypeText(context);
                    break;
                case CommandType.AppendText:
                    ExecuteAppendText(context);
                    break;
                case CommandType.ClearText:
                    ExecuteClearText(context);
                    break;
                case CommandType.SelectText:
                    ExecuteSelectText(context);
                    break;
                default:
                    base.Execute(context);
                    break;
            }
        }

        /// <summary>
        /// Executes the select text command
        /// </summary>
        /// <param name="context">The execution context used to execute this command</param>
        private void ExecuteSelectText(IExecutionContext context)
        {
            WatiN.Core.TextField element = new TextField(this.FindMethod.Execute(context));
            element.Select();
        }

        /// <summary>
        /// Executes the clear text command
        /// </summary>
        /// <param name="context">The execution context used to execute this command</param>
        private void ExecuteClearText(IExecutionContext context)
        {
            WatiN.Core.TextField element = new TextField(this.FindMethod.Execute(context));
            element.Clear();
        }

        /// <summary>
        /// Executes the append text command
        /// </summary>
        /// <param name="context">The execution context used to execute this command</param>
        private void ExecuteAppendText(IExecutionContext context)
        {
            WatiN.Core.TextField element = new TextField(this.FindMethod.Execute(context));
            element.AppendText(this.Text);
        }

        /// <summary>
        /// Executes the type text command
        /// </summary>
        /// <param name="context">The execution context used to execute this command</param>
        private void ExecuteTypeText(IExecutionContext context)
        {
            WatiN.Core.TextField element = new TextField(this.FindMethod.Execute(context));
            element.TypeText(this.Text);
        }

        /// <summary>
        /// Gets the text associated with this command.
        /// </summary>
        /// <value>The text.</value>
        private string Text
        {
            get
            {
                string textValue = string.Empty;
                if (this.Arguments.Count > 0)
                {
                    textValue = this.Arguments[0];
                }

                return textValue;
            }
        }
    }
}
